package com.bright.hxj.qchz.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bright.hxj.qchz.mapper.Duijie_ShiJieShouDeploy_TablepanaMapper;
import com.bright.hxj.qchz.pojo.po.Duijie_ShiJieShouDeploy;
import com.bright.hxj.qchz.mapper.Duijie_ShiJieShouDeployMapper;
import com.bright.hxj.qchz.pojo.po.Duijie_ShiJieShouDeploy_TablePana;
import com.bright.hxj.qchz.service.Duijie_ShiJieShouDeployManager;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 数据对接接收主表配置 服务实现类
 * </p>
 *
 * @author hxj
 * @since 2025-01-06
 */
@Service
@RequiredArgsConstructor
@DS("datasource-shiJieShou")
public class Duijie_ShiJieShouDeployManagerImpl extends ServiceImpl<Duijie_ShiJieShouDeployMapper, Duijie_ShiJieShouDeploy> implements Duijie_ShiJieShouDeployManager {

    private final Duijie_ShiJieShouDeploy_TablepanaMapper tablePanaMapper;

    @Override
    public List<Duijie_ShiJieShouDeploy> getDeploys(String xmbm) {
        LambdaQueryWrapper<Duijie_ShiJieShouDeploy> queryWrapper = new QueryWrapper<Duijie_ShiJieShouDeploy>()
                .lambda()
                .eq(Duijie_ShiJieShouDeploy::getXmbm, xmbm);

        return listDeploys(queryWrapper);
    }

    @Override
    public List<Duijie_ShiJieShouDeploy> getMainDeploys() {
        // 清产核资特殊 主表xmbm!=type 且只有主表没有子表 用该方法返回所有deploy配置
        LambdaQueryWrapper<Duijie_ShiJieShouDeploy> queryWrapper = new QueryWrapper<Duijie_ShiJieShouDeploy>()
                .lambda();

        return listDeploys(queryWrapper);
    }

    private List<Duijie_ShiJieShouDeploy> listDeploys(LambdaQueryWrapper<Duijie_ShiJieShouDeploy> queryWrapper) {
        // 补充基本条件
        queryWrapper = queryWrapper.eq(Duijie_ShiJieShouDeploy::isVisible, true)
                .orderByAsc(Duijie_ShiJieShouDeploy::getDisid);
        List<Duijie_ShiJieShouDeploy> deploys = list(queryWrapper);
        for (Duijie_ShiJieShouDeploy rvalue : deploys) {

//        rvalue.setTablePanas(duijie_shiJieShouDeploy_tablePanaManager.getTablePanas(rvalue.getTablePanaName()));

            List<String> mainKeys = new ArrayList<String>();
            List<String> mainKeys_title = new ArrayList<String>();
            for(Duijie_ShiJieShouDeploy_TablePana tablePana: rvalue.getTablePanas()) {
                if(tablePana.getIsMainKey()) {
                    mainKeys.add(tablePana.getTcolumn());
                }
            }
            rvalue.setMainKeys(mainKeys);
            rvalue.setMainKeys_title(mainKeys_title);
        }

        return deploys;
    }
}
